package ooday03;

public class PolymorphicDemo {
    public static void main(String[] args) {
        //演示强转成功的条件
        Animal o=new Dog("小黑",2,"黑");//向上造型
        Dog g=(Dog)o;//引用o所指对象。就是Dog类型
        Swim s=(Swim)o;///引用o所指对象
        //Fish f=(fish)o;
        System.out.println(o instanceof Dog);//true
        System.out.println(g instanceof Swim);//true
        System.out.println(s instanceof Fish);//false
        if (o instanceof Fish) {//false
            Fish f=(Fish)o;
        }else{
            System.out.println("o不是Fish类型");
        }


        //演示向上造型多态的第一点应用；
       /* Master master=new Master();
        Animal dog=new Dog("小黑",5,"黑");
        Animal chick=new Chick("小白",4,"白");
        Animal Fish=new Fish("小灰",3,"灰");
        master.feed(dog);//传参的同时系统做了向上造型 Animal animal=dog；
        master.feed(chick);
        master.feed(Fish);
               */
       //演示向上造型多态的第二点应用；
        Animal[]animals=new Animal[5];
        animals[0]=new Dog("小黑",3,"黑");//向上造型
        animals[1]=new Dog("小白",2,"白");
        animals[2]=new Fish("小红",4,"红");
        animals[3]=new Fish("小花",4,"花");
        animals[4]=new Chick("小灰",6,"灰");

        for(int i=0;i<animals.length;i++){//遍历所有动物
            System.out.println(animals[i].name);
            animals[i].eat();
            animals[i].drink();
            if(animals[i] instanceof Dog){
                Dog dog=(Dog)animals[i];
                dog.lookHome();
            }
            if(animals[i] instanceof Chick){
                Chick chick=(Chick)animals[i];
                chick.layEggs();
            }
            if (animals[i] instanceof Swim){
                Swim k=(Swim)animals[i];
                k.swim();
            }
        }









        //演示向上造型
        /*
        Dog o1=new Dog("小黑",3,"黑");
        Animal o2=new Dog("小黑",3,"黑");
        Swim o3=new Dog("小黑",3,"黑");

         */
    }
}
